#include <pspdebug.h>
#include <pspkernel.h>
#include <string>
#include <stdio.h>
#include "../Common/Callbacks.h"
#include "../Epik_Controller.h"
#include "../Epik_Input.h"

PSP_MODULE_INFO("Epik_Controller DEMO", 0, 1, 1);

enum ZONES {
	EDITOR,
	OPTIONS
};
typedef struct {
	int x_;
	int y_;
} Point;

const char *menu[4] = {"NEW","OPEN","SAVE","QUIT"};

void InsertText(char scr[][58], const char *txt, Point *p);
void RefreshScreen(char scr[][58]);

//EPIK TEXT EDITOR--------------------------------------------
int main()
{
	SetupCallbacks();
	pspDebugScreenInit();

	char scrBuf[25][58];
	char *filename = {0};
	int focus = OPTIONS;
	int menuID = 0;
	bool insertMode = true;
	bool padLeft, padRight, padUp, padDown;
	bool btnTr, btnX, btnSTART;

	FILE *f;
	Point cursor;
	Epik_Controller ec(&_running);
	Epik_Input ei("Text input");

	ec.BindButton(Epik_Controller::BTN_CROSS, &btnX);
	ec.BindButton(Epik_Controller::BTN_TRIANGLE, &btnTr);
	ec.BindButton(Epik_Controller::HUB_START, &btnSTART);

	ec.BindButton(Epik_Controller::DPAD_UP, &padUp);
	ec.BindButton(Epik_Controller::DPAD_DOWN, &padDown);
	ec.BindButton(Epik_Controller::DPAD_LEFT, &padLeft);
	ec.BindButton(Epik_Controller::DPAD_RIGHT, &padRight);

	do
	{
		ec.UpdateButtons();

		if (btnTr)
		{
			focus = (focus*(-1))+1;

			pspDebugScreenSetXY(51,28);
			if (insertMode) {
				pspDebugScreenSetTextColor(0x00FF00);
				pspDebugScreenPrintf("Insert ");
			} else {
				pspDebugScreenSetTextColor(0x0000FF);
				pspDebugScreenPrintf("Replace");
			}
		}

		if (btnSTART)
		{
			menuID = 0;
			focus = focus*(-1) + 1;
		}

		switch (focus)
		{
		case EDITOR:
			if (padUp && cursor.x_>0)
				cursor.x_--;
			if (padDown && cursor.x_<25)
				cursor.x_++;
			if (padLeft && cursor.y_>0)
				cursor.y_--;
			if (padRight && cursor.y_<58)
				cursor.y_++;
			if (btnX)
				InsertText(scrBuf, ei.GetInput().c_str(), &cursor);
			break;

		case OPTIONS:
			if (padLeft && menuID > 0)
				menuID--;
			if (padRight && menuID < 4)
				menuID++;
			if (btnX)
				switch(menuID)
				{
				case 0:	// NEW
					filename = const_cast<char *>(ei.GetInput().c_str());
					RefreshScreen(scrBuf);
					break;

				case 1: // OPEN
					break;

				case 2: // SAVE
					if (!filename)
						filename = const_cast<char *>(ei.GetInput().c_str());
					f = fopen(filename,"w");
					for (int i=0; i<25; i++)
						fprintf(f,"%s\n",scrBuf[i]);
					fclose(f);

					break;

				case 3: // QUIT
					break;

				default:	// WTF, this shouldn't happen, lets reset the menu.
					menuID = 0;
					break;
				};
			break;

		default:
			break;
		}

		sceKernelDelayThread(1000);
	} while (_running);

	sceKernelExitGame();
}

void RefreshScreen(char scr[][58])
{
	pspDebugScreenClear();
	pspDebugScreenSetTextColor(0x0000FF);
	pspDebugScreenPrintf("+-EPIK TEXT EDITOR-----------------------------------------+\n");
	for (int i=0; i<25; i++)
		pspDebugScreenPrintf("|                                                          |\n");
	pspDebugScreenPrintf("+----------------------------------------------------------+\n");
	pspDebugScreenPrintf("+-  OPTIONS------------------------------------------------+\n");
	pspDebugScreenPrintf("|                                                          |\n");
	pspDebugScreenPrintf("+----------------------------------------------------------+\n");
}

void InsertText(char screen[][58], const char *text, Point *cur)
{
	int i;
	for( i=cur->x_; i<58 && text[i-cur->x_]; i++)
		screen[cur->y_][i] = text[i-cur->x_];

	if (cur->x_ + i <= 58) {
		cur->x_ = 0;
		cur->y_++;
	} else {
		cur->x_ += i;
	}
}


